package com.click.app.model;

import com.click.app.util.CurrencyConverter;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author orgale
 */
@Entity
@Table(name = "storage_materials")
public class StorageMaterial extends BaseObject {

    private Long id;
    private Material material;
    private Float stock;
    private Float limit;  
    private List<PackageDetail> packageMaterials = new ArrayList<PackageDetail>();


    public StorageMaterial(){
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id_storage_material", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="id_material")
    public Material getMaterial() {
        return this.material;
    }

    public void setMaterial(Material material) {
        this.material = material;
    }

    @Column(name = "stock", precision = 12, scale = 0)
    public Float getStock() {
        return this.stock;
    }

    public void setStockFormatted(String stockFormatted){
        System.out.println("stock definido en set: "+getStock());
        CurrencyConverter cc = new CurrencyConverter();
        Double aux = (Double) cc.convert(Double.class, stockFormatted);
        setStock(Float.parseFloat(aux.toString()));
    }

    @Transient
    public String getStockFormatted() {
        System.out.println("stock definido en get: "+getStock());
        CurrencyConverter cc = new CurrencyConverter();
        System.out.println("retornando: "+((String) cc.convert(String.class, (Double.parseDouble(getStock().toString())))));
        return ((String) cc.convert(String.class, (Double.parseDouble(getStock().toString()))));
        }


    public void setStock(Float stock) {
        this.stock = stock;
    }

    @Column(name = "stock_limit", precision = 12, scale = 0)
    public Float getLimit() {
        return this.limit;
    }

    public void setLimit(Float limit) {
        this.limit = limit;
    }

    public void setLimitFormatted(String limitFormatted){
        CurrencyConverter cc = new CurrencyConverter();
        Double aux = (Double) cc.convert(Double.class, limitFormatted);
        setLimit(Float.parseFloat(aux.toString()));
    }

    @Transient
    public String getLimitFormatted() {
        CurrencyConverter cc = new CurrencyConverter();
        return ((String) cc.convert(String.class, (Double.parseDouble(getLimit().toString()))));
        }

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "storageMaterial")
    public List<PackageDetail> getPackageMaterials() {
        if (packageMaterials==null){
            packageMaterials=new ArrayList<PackageDetail>();
        }
        return packageMaterials;
    }

    public void setPackageMaterials(List<PackageDetail> packageMaterials) {
        this.packageMaterials = packageMaterials;
    }

    public void incrementStok( Float quantity ){
        this.stock = stock+quantity;
    }

    public void decrementStok( Float quantity ){
        this.stock = stock-quantity;
    }

    @Override
    public String toString() {
        return id+","+stock+","+limit;
    }

    @Override
    public boolean equals(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int hashCode() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
